package com.lamatek.tags.google;

import java.io.Serializable;

import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;

import com.lamatek.tags.google.beans.EscapeChars;

/**
 * GoogleMapTabTag
 * 
 * This tag represents a &lt;googlemaps:tab> tag, which is a child to &lt;googlemaps:tabbedInfoWindow> tag.
 * Developers should not extend this class or override it's methods.
 * 
 * @author Tom Cole
 * @version 0.87
 */
public class GoogleMapTabTag extends BodyTagSupport implements Serializable {

    String label = null;
    String content = null;
    boolean html = true;
    /**
     * Overrides doStartTag() from BodyTagSupport.
     */
    public int doStartTag() {
        return EVAL_BODY_BUFFERED;
    }
    /**
     * Overrides doEndTag from BodyTagSupport.
     */
    public int doEndTag() {
        if (content == null) {
            content = getBodyContent().getString().trim();
            if (html)
                content = EscapeChars.escape(content);
        }
        Tag tag = this;
        while (tag.getParent() != null) {
            if (tag.getParent() instanceof GoogleMapInfoWindowTabbedTag) {
                ((GoogleMapInfoWindowTabbedTag) tag.getParent()).addTab(this);
                return EVAL_PAGE;
            }
            tag = tag.getParent();
        }
        return EVAL_PAGE;
    }
    /**
     * Returns the label for this tab. The label is the clickable text that is
     * displayed on a tab to show it's contents.
     * 
     * @return The label for this tab.
     */
    public String getLabel() {
        return label;
    }
    /**
     * Sets the label for this tab. The label is the clickable text that is
     * displayed on a tab to show it's contents.
     * 
     * @param label The label for this tab.
     */
    public void setLabel(String label) {
        this.label = label;
    }
    /**
     * Returns the displayable contents of this tab. This can be HTML or plain text.
     * These contents are displayed when this tab is clicked.
     * 
     * @return The contents of this tab.
     */
    public String getContent() {
        return content;
    }
    /**
     * Sets the displayable contents of this tab. This can be HTML or plain text.
     * These contents will be displayed when this tab is clicked. If this is set,
     * the content between the starting tag and ending tag will be ignored.
     * 
     * @param content The contents of this tab.
     */
    public void setContent(String content) {
        if (html) {
            this.content = EscapeChars.escape(content.trim());
        }
        else {
            this.content = content.trim();
        }
    }
    /**
     * Denotes whether or not the contents of this tag are html and therefore
     * need to be escaped.
     * 
     * @return True or false.
     */
    public boolean isHtml() {
        return html;
    }
    /**
     * Sets whether or not the contents of this tag are html and therefore need
     * to be escaped.
     * 
     * @param html True if content needs to be escaped, false if not.
     */
    public void setHtml(boolean html) {
        this.html = html;
    }
}
